package com.ymatou.iapi.sellerquery.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.query.seller.trading.api.parameter.GetBlackBuyerListBean;
import com.ymatou.query.seller.trading.api.parameter.GetBlackBuyerListResponse;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.model.Ymt_BlackListForSeller;
import com.ymttest.database.sqlwapper.YmtBlackListForSellerWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;

/**
 * 获取卖家黑名单用户 FreeMaker Template自动生成代码
 * 接口已经转接java
 */
@TestSuite
public class Ts_GetBlackBuyerList {

	private static GetBlackBuyerListBean getblackbuyerlistbean;
//	private static com.ymatou.query.seller.trading.api.service.GetBlackBuyerListCall getblackbuyerlistcall;
	private static com.ymatou.iapi.trading.sellerquery.service.GetBlackBuyerListCall getblackbuyerlistcall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取卖家黑名单用户");
	}

	@Before
	public void caseUp() {
		getblackbuyerlistbean = new GetBlackBuyerListBean();
//		getblackbuyerlistcall = new com.ymatou.query.seller.trading.api.service.GetBlackBuyerListCall();
		getblackbuyerlistcall = new com.ymatou.iapi.trading.sellerquery.service.GetBlackBuyerListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getblackbuyerlistcall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {
		GetBlackBuyerListResponse expectResponse = new GetBlackBuyerListResponse();
		ConstructExpectedResponse(expectResponse);
		GetBlackBuyerListResponse actualResponse = getblackbuyerlistcall
				.getBlackBuyerListResponse();
		List<GetBlackBuyerListResponse.BlackBuyerInfo> expects = expectResponse
				.getBlackBuyerList();
		List<GetBlackBuyerListResponse.BlackBuyerInfo> actuals = actualResponse
				.getBlackBuyerList();
		DataUtil.verifyEquals(expects.size(), actuals.size(),
				"验证BlackBuyerList的数量");
		for (GetBlackBuyerListResponse.BlackBuyerInfo actual : actuals) {
			int actBid = actual.getBuyerId();
			int actSid = actual.getSellerId();
			boolean existsInExpects = expects.stream().anyMatch(
					x -> x.getBuyerId() == actBid && x.getSellerId() == actSid);
			DataUtil.verifyEquals(true, existsInExpects, "验证BuyerId: " + actBid
					+ ", SellerId: " + actSid + "是否存在于期望结果中");
		}
	}

	private void ConstructExpectedResponse(GetBlackBuyerListResponse response) {
		List<Ymt_BlackListForSeller> results = execute();
		ConvertResult(response, results);
	}

	private void ConvertResult(GetBlackBuyerListResponse response,
			List<Ymt_BlackListForSeller> results) {
		List<GetBlackBuyerListResponse.BlackBuyerInfo> blackBuyerList = new ArrayList<GetBlackBuyerListResponse.BlackBuyerInfo>();
		if (DataUtil.isNullOrEmpty(results) == false) {
			for (Ymt_BlackListForSeller o : results) {
				GetBlackBuyerListResponse.BlackBuyerInfo i = new GetBlackBuyerListResponse.BlackBuyerInfo();
				i.setBuyerId(o.getiBuyerId());
				i.setSellerId(o.getiSellerId());
				blackBuyerList.add(i);
			}
		}
		response.setBlackBuyerList(blackBuyerList);

	}

	private List<Ymt_BlackListForSeller> execute() {
		YmtBlackListForSellerWapper wapper = new YmtBlackListForSellerWapper();
		String query = " iSellerId = "
				+ DataUtil.Integerr.GetValueOrDefault(getblackbuyerlistbean
						.getSellerId()) + " and iAction = 0";
		return wapper.selectBlackListForSeller(null, null, query);
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBlackBuyerList_001() {
		Logger.start(true, "获取卖家黑名单用户成功");
		try {
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			getblackbuyerlistbean.setSellerId(SellerId);
			// 发送请求
			String param = FormatUtil.beanToHttpString(getblackbuyerlistbean,
					true, false);
			getblackbuyerlistcall.setData(param);
			getblackbuyerlistcall.callService();
			Logger.comment("getblackbuyerlistcall send:"
					+ getblackbuyerlistcall.getEntityString());
			Logger.comment("getblackbuyerlistcall response:"
					+ getblackbuyerlistcall.getReturnData());
			// Logger.verifyEquals(ECode.SUCCESS,
			// getblackbuyerlistcall.getCode(),
			// "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetBlackBuyerList_002() {
		Logger.start(true, "卖家不存在,查询结果为空");
		try {
			int SellerId = 288;
			getblackbuyerlistbean.setSellerId(SellerId);
			// 发送请求
			String param = FormatUtil.beanToHttpString(getblackbuyerlistbean,
					true, false);
			getblackbuyerlistcall.setData(param);
			getblackbuyerlistcall.callService();
			Logger.comment("getblackbuyerlistcall send:"
					+ getblackbuyerlistcall.getEntityString());
			Logger.comment("getblackbuyerlistcall response:"
					+ getblackbuyerlistcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, getblackbuyerlistcall.getCode(),
					"验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetBlackBuyerList_003() {
		Logger.start(true, "卖家为空,查询结果为空");
		try {
			String param = FormatUtil.beanToHttpString(getblackbuyerlistbean,
					true, false);
			getblackbuyerlistcall.setData(param);
			getblackbuyerlistcall.callService();
			Logger.comment("getblackbuyerlistcall send:"
					+ getblackbuyerlistcall.getEntityString());
			Logger.comment("getblackbuyerlistcall response:"
					+ getblackbuyerlistcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, getblackbuyerlistcall.getCode(),
					"验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}